#include "chatroom.h"
#include <QMessageBox>

Chatroom::Chatroom(QObject *parent) : QObject(parent)
{
    this->log_menu = new Log; 
    connect(this->log_menu, &Log::login_ok, this, [=](QString msg, QTcpSocket *cfd)
    {
        this->chat_menu = new Chat(msg, nullptr, cfd); 
        this->log_menu->close(); 
        this->chat_menu->show(); 
    }); 

    connect(this->log_menu, &Log::disconnect_ok, this, &Chatroom::mdisconnect); 

    this->log_menu->show(); 
    qDebug() << "----------------------"; 
    delete chat_menu; 
}

Chatroom::~Chatroom()
{
    qDebug() << "chatroom dead"; 
    if(this->log_menu) disconnect(this->log_menu, &Log::disconnect_ok, this, &Chatroom::mdisconnect); 
    delete log_menu; 
    delete chat_menu; 
}

void Chatroom::mdisconnect()
{
    qDebug() << "disconnect_ok"; 
    QMessageBox::information(nullptr, "通知", "已与服务器断开");        
    if(!this->log_menu->isHidden()) this->log_menu->close(); 
    if(this->chat_menu) this->chat_menu->close(); 
    //delete this->log_menu;
    this->log_menu->deleteLater(); 
    this->log_menu = nullptr; 
    //delete this->chat_menu; 
    this->chat_menu->deleteLater(); 
    this->chat_menu = nullptr; 
}